package com.sky.service.user.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.mapper.user.UserCategoryMapper;
import com.sky.pojo.Category;
import com.sky.service.user.USerCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class USerCategoryServiceImpl implements USerCategoryService {
    @Autowired
    private UserCategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Category> queryList() {
        String jsonString = (String)redisTemplate.opsForValue().get("CATEGORY-LIST");
        if (StringUtils.hasText(jsonString)){
            return JSONObject.parseArray(jsonString, Category.class);
        }
        log.info("redis中不存在分类，从数据库中查询");
        List<Category> list = categoryMapper.queryList();
        redisTemplate.opsForValue().set("CATEGORY-LIST", JSON.toJSONString(list));
        return list;
    }
}
